﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using WebStudio.Business.Entity;
using WebStudio.Framework.Helper;

namespace WebStudio.Business.Rule
{
    public class MessageRule
    {
        #region 公有方法
        /// <summary>
        /// 获取信息列表
        /// </summary>
        /// <returns></returns>
        public static List<MessageEntity> GetMessageList()
        {
            List<MessageEntity> list = new List<MessageEntity>();
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM MessageTB")))
            {
                return FillDate(dr);
            }
        }

        public static List<MessageEntity> GetMessageList(int tops,string strWhere)
        {
            List<MessageEntity> list = new List<MessageEntity>();
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT top {0} * FROM MessageTB where {1} order by SubmitDate desc", tops, strWhere)))
            {
                return FillDate(dr);
            }
        }
        public static List<MessageEntity> GetMessageList(int pageIndex, int pageSize, int pageCount,int recordCount, string strWhere)
        {
            List<MessageEntity> list = new List<MessageEntity>();
            string strSql = string.Empty;
            if (pageIndex >= pageCount)
            {
                if (recordCount == 0)
                {
                    strSql = "select * from (select top " + pageSize + " * from (select top " + pageIndex * pageSize + " * FROM MessageTB where " + strWhere + " order by MessageId desc) order by MessageId) order by MessageId desc";
                }
                else
                {
                    strSql = "select * from (select top " + (recordCount - (pageIndex - 1) * pageSize) + " * from (select top " + pageIndex * pageSize + " * FROM MessageTB where " + strWhere + " order by MessageId desc) order by MessageId) order by MessageId desc";
                }
            }
            else
            {
                strSql = "select * from (select top " + pageSize + " * from (select top " + pageIndex * pageSize + "  * FROM MessageTB where " + strWhere + " order by MessageId desc) order by MessageId) order by MessageId desc";
            }
            using (var dr = DBHelper.ExecuteReader(strSql))
            {
                return FillDate(dr);
            }
        }

        public static int GetMessageListCount(string strWhere)
        {
            return DBHelper.ExecuteIntScalar(string.Format("SELECT count(*) FROM MessageTB where {0}", strWhere));
        }
        /// <summary>
        /// 根据条件获取信息列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static List<MessageEntity> GetMessageListByCondition(string condition)
        {
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM MessageTB WHERE {0}", condition)))
            {
                return FillDate(dr);
            }
        }

        /// <summary>
        /// 获取信息实体
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static MessageEntity GetMessageEntity(string condition)
        {
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM MessageTB WHERE {0}", condition)))
            {
                List<MessageEntity> list = FillDate(dr);
                return list == null || list.Count == 0 ? (new MessageEntity()) : list[0];
            }
        }

        /// <summary>
        /// 根据标识获取信息实体重载一
        /// </summary>
        /// <param name="messageId"></param>
        /// <returns></returns>
        public static MessageEntity GetMessageEntity(int messageId)
        {
            return GetMessageEntity(string.Format("MessageId = {0}", messageId));
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="te"></param>
        /// <returns></returns>
        public static bool SaveMessageInfor(MessageEntity te)
        {
            int i = 0;
            if (te.MessageId <= 0)
            {
                i = DBHelper.ExecuteNonQuery(string.Format("INSERT INTO MessageTB (MessageTitle,MessageContent,OwenerIP,SubmitDate,UserName,Phone,Email) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", te.MessageTitle, te.MessageContent, te.OwenerIP, te.SubmitDate, te.UserName,te.Phone,te.Email));
            }
            else
            {
                i = DBHelper.ExecuteNonQuery(string.Format("UPDATE MessageTB SET Status ={0} ,ReplyContent ='{1}',ReplyUserName='{2}',ReplyDate='{3}' WHERE MessageId = {4}", te.Status, te.ReplyContent, te.ReplyUserName, te.ReplyDate, te.MessageId));
            }
            return i > 0 ? true : false;
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool DeleteMessageInfor(int messageId)
        {
            return DBHelper.ExecuteNonQuery(string.Format("DELETE FROM MessageTB WHERE MessageId = {0}", messageId)) >= 0 ? true : false;
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private static List<MessageEntity> FillDate(IDataReader dr)
        {
            List<MessageEntity> list = new List<MessageEntity>();
            MessageEntity te;
            while (dr != null && dr.Read())
            {
                te = new MessageEntity();
                te.MessageId = ConvertHelper.IntParse(dr["MessageId"]);
                te.MessageTitle = ConvertHelper.StringParse(dr["MessageTitle"]);
                te.MessageContent = ConvertHelper.StringParse(dr["MessageContent"]);
                te.OwenerIP = ConvertHelper.StringParse(dr["OwenerIP"]);
                te.UserName = ConvertHelper.StringParse(dr["UserName"]);
                te.SubmitDate = ConvertHelper.DateTimeParse(dr["SubmitDate"]);
                te.Status = ConvertHelper.IntParse(dr["Status"]);
                te.Phone = ConvertHelper.StringParse(dr["Phone"]);
                te.Email = ConvertHelper.StringParse(dr["Email"]);
                te.ReplyContent = ConvertHelper.StringParse(dr["ReplyContent"]);
                te.ReplyUserName = ConvertHelper.StringParse(dr["ReplyUserName"]);
                object obj = dr["ReplyDate"];
                if (obj != null && obj != DBNull.Value)
                {
                    te.ReplyDate = ConvertHelper.DateTimeParse(dr["ReplyDate"]);
                }
                
                list.Add(te);
            }
            return list;
        }
        #endregion
    }
}
